package com.eascs.web.o2o.web.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.web.o2o.web.form.O2oBPAfterLoanForm;
import com.eascs.web.o2o.web.common.BaseMasterViewController;
//import com.eascs.admin.o2o.credit.service.CreditReportService;
//import com.eascs.admin.o2o.internal.service.AuditCommentService;
import com.eascs.app.dao.PageResult;
import com.eascs.common.doc.excel.DefaultRowWriteHandler;
import com.eascs.common.doc.excel.ExcelApiHelper;
import com.eascs.common.exception.RuntimeIOException;
import com.eascs.common.util.IoUtil;
import com.eascs.common.util.UrlUtil;
//import com.eascs.crm.common.service.ICustomerInfoService;
//import com.eascs.datalist.service.O2oDataListService;
//import com.eascs.loan.service.O2oPaymentsService;
import com.eascs.web.o2o.web.service.O2oPBAfterService;
import com.eascs.site.view.ViewContext;
import com.eascs.tag.MenuTag;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sys.SysOrgService;

@Controller
@RequestMapping("/pbafterloan")
public class O2oBPAfterLoanController extends BaseMasterViewController {

	@Override
	public void preHandle() {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "pbafterloan");
	}
	
	@Autowired
	O2oPBAfterService service;

	@Reference
	SysOrgService sysOrgService;
	

	@RequestMapping("/index")
	public String index(O2oBPAfterLoanForm entity) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "pbafterloan");
		String o2ourl = "/pbrafterloan/o2o";
		String reporto2ourl = "/pbafterloan/download";
		
		List<ActOrgEntity> orgProvince = sysOrgService.getOrgProvince();
		ViewContext.put("ActOrgEntitys", orgProvince);
		if (entity != null) {
			String billCode = entity.getBillCode() == null ? "" : entity.getBillCode();
			String bankLoan = entity.getBankLoan() == null ? "" : entity.getBankLoan();
			String operName = entity.getOperName() == null ? "" : entity.getOperName();
			String loanUserName = entity.getLoanUserName() == null ? "" : entity.getLoanUserName();
			String loanDateStart = entity.getLoanDateStart() == null ? null : entity.getLoanDateStart();
			String loanDateEnd = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
			String createUserName = entity.getCreateUserName() == null ? "" : entity.getCreateUserName();
			String createDateStart = entity.getCreateDateStart() == null ? null : entity.getCreateDateStart();
			String createDateEnd = entity.getCreateDateEnd() == null ? null : entity.getCreateDateEnd();
			String loanType = entity.getLoanType() == null ? "" : entity.getLoanType();
			String ltd = entity.getLtd() == null ? "" : entity.getLtd();
			String ltdName = entity.getLtdName() == null ? "" : entity.getLtdName();
			String province = entity.getProvince() == null ? "" : entity.getProvince();
			String gdStatus = entity.getGdStatus() == null ? "" : entity.getGdStatus();
			String repaystatus = entity.getRepaystatus() == null ? "" : entity.getRepaystatus();
			String dateDueStart = entity.getDateDueStart() == null ? null : entity.getDateDueStart();
			String dateDueEnd = entity.getDateDueEnd() == null ? null : entity.getDateDueEnd();
			String idc = entity.getIdc() == null ? null : entity.getIdc();
			String regionalName = entity.getRegionalName() == null ? "" : entity.getRegionalName();
			String brandName = entity.getBrandName() == null ? "" : entity.getBrandName();
			String partnerName = entity.getPartnerName() == null ? "" : entity.getPartnerName();
			String hostProvince=entity.getHostProvince()==null?"":entity.getHostProvince();
			o2ourl = UrlUtil.appendUrl(o2ourl, "billCode", billCode);
			o2ourl = UrlUtil.appendUrl(o2ourl, "bankLoan", bankLoan);
			o2ourl = UrlUtil.appendUrl(o2ourl, "operName", operName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanUserName", loanUserName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createUserName", createUserName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createDateStart", createDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createDateEnd", createDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "dateDueStart", dateDueStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "dateDueEnd", dateDueEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "ltdName", ltdName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "ltd", ltd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanType", loanType);
			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			o2ourl = UrlUtil.appendUrl(o2ourl, "gdStatus", gdStatus);
			o2ourl = UrlUtil.appendUrl(o2ourl, "repaystatus", repaystatus);
			o2ourl = UrlUtil.appendUrl(o2ourl, "idc", idc);
			o2ourl = UrlUtil.appendUrl(o2ourl, "regionalName", regionalName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "brandName", brandName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "partnerName", partnerName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "hostProvince", hostProvince);
			// UrlUtil.resetParam(RequestContext.getRequestUrlWithQueryString(),
			// "pageOldIndex", "")
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "billCode", billCode);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "bankLoan", bankLoan);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "operName", operName);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "loanUserName", loanUserName);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "loanDateStart", loanDateStart);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "loanDateEnd", loanDateEnd);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "createUserName", createUserName);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "createDateStart", createDateStart);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "createDateEnd", createDateEnd);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "dateDueStart", dateDueStart);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "dateDueEnd", dateDueEnd);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "ltdName", ltdName);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "ltd", ltd);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "loanType", loanType);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "province", province);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "gdStatus", gdStatus);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "repaystatus", repaystatus);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "idc", idc);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "regionalName", regionalName);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "brandName", brandName);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "partnerName", partnerName);
			reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "hostProvince", hostProvince);
			o2ourl = o2ourl + "&oaIndex=";
			o2ourl = o2ourl + "&o2oIndex=";
		} else {
			o2ourl = o2ourl + "?o2oIndex=";
		}
		ViewContext.put("o2ourl", o2ourl);
		ViewContext.put("reporto2ourl", reporto2ourl);
		ViewContext.put("vo", entity);
		return "pbafterloan/index";
	}
	
	@RequestMapping("/download")
	public void dowanload(O2oBPAfterLoanForm entity, HttpServletResponse httpServletResponse, HttpServletRequest request) {
		entity.setPageSize(8000);
		PageResult<Object[]> pageResult = service.getO2oAfterLoanExport(entity);
		String path = O2oBPAfterLoanController.class.getResource("/excelTemplate").getPath();
		StringBuffer s = new StringBuffer();
		String outPath = s.append(path).append("/BPAfterLoan-model.xls").toString();
		OutputStream os = null;
		try {
			String outfile = "品牌贷放款数据.xls";
			String userAgent = request.getHeader("User-Agent");
			byte[] bytes = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
			outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
			File f = new File(outPath);
			if (!f.exists())
				throw new RuntimeIOException("file not found");
			os = httpServletResponse.getOutputStream();
			httpServletResponse.reset();
			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
			DefaultRowWriteHandler defaultRowWriteHandler = new DefaultRowWriteHandler(pageResult.getData());
			ExcelApiHelper.writeHandle(httpServletResponse.getOutputStream(), O2oBPAfterLoanController.class.getResourceAsStream("/excelTemplate/BPAfterLoan-model.xls"), defaultRowWriteHandler);
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			IoUtil.fileToOutputStream(f, byteArrayOutputStream);
			os.write(byteArrayOutputStream.toByteArray());
			os.flush();
			os.close();
		} catch (Exception e) {
			throw new RuntimeIOException(e);
		} finally {
			IoUtil.close(os);
		}
	}
	
}
